<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      @submit.native.prevent
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px">
      <el-form-item prop="className">
        <el-input
          v-model="queryParams.className"
          :placeholder="$t('分类名称')"
          clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd()"
          v-hasPermi="['distribution:skuClass:add']">
          新增
        </el-button>
        <el-button type="primary" plain size="mini" @click="handleImportSku"> sku分类初始化 </el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      ref="TableRef"
      :data="tableData"
      row-key="id"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      @selection-change="handleSelectionChange">
      <el-table-column show-overflow-tooltip label="分类中文名" align="left" prop="className" />
      <el-table-column show-overflow-tooltip label="分类日文名" align="left" prop="classNameJp" />
      <el-table-column show-overflow-tooltip label="分类英文名" align="left" prop="classNameEn" />
      <!-- <el-table-column show-overflow-tooltip label="关税税率" align="center" prop="tariffRate" /> -->
      <el-table-column show-overflow-tooltip :label="$t('备注')" align="center" prop="remark" />
      <el-table-column show-overflow-tooltip :label="$t('操作')" align="center" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['distribution:skuClass:edit']">
            修改
          </el-button>
          <el-button
            type="text"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd(scope.row)"
            v-hasPermi="['distribution:skuClass:add']">
            新增
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['distribution:skuClass:remove']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加或修改商品SKU分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="上级分类" prop="parentId">
          <treeselect
            v-model="form.parentId"
            :options="menuOptions"
            :show-count="true"
            :placeholder="$t('请选择')" />
        </el-form-item>
        <el-form-item label="分类中文名" prop="className">
          <el-input v-model="form.className" />
        </el-form-item>
        <el-form-item label="分类日文名" prop="classNameJp">
          <el-input v-model="form.classNameJp" />
        </el-form-item>
        <el-form-item label="分类英文名" prop="classNameEn">
          <el-input v-model="form.classNameEn" />
        </el-form-item>
        <!-- <el-form-item label="关税税率" prop="tariffRate">
          <el-input type="number" v-model.number="form.tariffRate" />
        </el-form-item> -->
        <el-form-item :label="$t('备注')">
          <el-input v-model="form.remark" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <ImportSku
      templateUrl="/distribution/skuClass/initSkuClassTemp"
      action="/distribution/skuClass/initSkuClass"
      :show.sync="importSku.show"
      :formData="importSku.formData" />
  </div>
</template>

<script>
import {
  listSkuClass,
  getSkuClass,
  delSkuClass,
  addSkuClass,
  updateSkuClass,
} from '@/api/platform/goodsClassDict'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import ImportSku from './components/importSku.vue'
export default {
  activated() {
    this.$nextTick(() => this.$refs['TableRef']?.doLayout())
  },
  name: 'GoodsClassDict',
  components: { Treeselect, ImportSku },
  data() {
    return {
      importSku: {
        show: false,
        formData: {},
      },
      // 菜单树选项
      menuOptions: [],
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 商品SKU分类表格数据
      tableData: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        // pageNum: 1,
        // pageSize: 10,
        className: undefined,
        tariffRate: undefined,
        tariffAmount: undefined,
        classKey: undefined,
        parentId: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        className: [{ required: true, message: '请输入', trigger: 'blur' }],
        classNameJp: [{ required: true, message: '请输入', trigger: 'blur' }],
        classNameEn: [{ required: true, message: '请输入', trigger: 'blur' }],
        tariffRate: [{ required: true, message: '关税税率不能为空', trigger: 'blur' }],
        parentId: [{ required: true, message: '父菜单ID不能为空', trigger: 'blur' }],
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleImportSku() {
      this.importSku.show = true
    },
    /** 查询菜单下拉树结构 */
    getTreeselect() {
      listSkuClass().then((response) => {
        this.menuOptions = []
        const menu = { id: 0, label: '主类目', children: [] }
        menu.children = this.handleTree(response.data, 'id')
        this.menuOptions.push(menu)
      })
    },

    /** 查询商品SKU分类列表 */
    getList() {
      listSkuClass(this.queryParams).then((response) => {
        this.tableData = this.handleTree(response.data, 'id')
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        className: undefined,
        tariffRate: undefined,
        tariffAmount: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined,
        classKey: undefined,
        parentId: undefined,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset()
      if (row) {
        this.form.parentId = row.id
      }
      this.getTreeselect()
      this.open = true
      this.title = '添加商品SKU分类'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true
      this.reset()
      this.getTreeselect()

      const id = row.id
      getSkuClass(id).then((response) => {
        this.loading = false
        this.form = response.data
        this.open = true
        this.title = '修改商品SKU分类'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.buttonLoading = true
          if (this.form.id != null) {
            updateSkuClass(this.form)
              .then((response) => {
                this.$modal.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
              .finally(() => {
                this.buttonLoading = false
              })
          } else {
            addSkuClass(this.form)
              .then((response) => {
                this.$modal.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
              .finally(() => {
                this.buttonLoading = false
              })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否确认删除商品SKU分类编号为"' + ids + '"的数据项？')
        .then(() => {
          this.loading = true
          return delSkuClass(ids)
        })
        .then(() => {
          this.loading = false
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'distribution/skuClass/export',
        {
          ...this.queryParams,
        },
        `skuClass_${new Date().getTime()}.xlsx`,
      )
    },
  },
}
</script>
